package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class CalculateActivity extends AppCompatActivity {
    private TextView tvTitle;
    private EditText etRmb;
    private TextView tvResult;
    private double sellRate;

    public static void start(Context context, Currency currency) {
        Intent intent = new Intent(context, CalculateActivity.class);
        intent.putExtra("currency_name", currency.getCurrencyName());
        intent.putExtra("sell_rate", currency.getSellRate());
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);

        tvTitle = findViewById(R.id.tv_title);
        etRmb = findViewById(R.id.et_rmb);
        tvResult = findViewById(R.id.tv_result);

        String currencyName = getIntent().getStringExtra("currency_name");
        sellRate = getIntent().getDoubleExtra("sell_rate", 0);
        tvTitle.setText(currencyName + " 汇率计算（卖出价）");

        etRmb.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void updateResult() {
        String input = etRmb.getText().toString().trim();
        if (input.isEmpty()) {
            tvResult.setText("");
            return;
        }
        try {
            double rmb = Double.parseDouble(input);
            double result = rmb * sellRate;  // 人民币 × 卖出价（1人民币=X外币）
            tvResult.setText(String.format("可兑换：%.4f 外币", result));
        } catch (NumberFormatException e) {
            tvResult.setText("输入格式错误");
        }
    }
}